package com.clarence.login.controller;

import com.clarence.admin.service.UserService;
import com.clarence.login.vo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author clarence
 * @create 2019-10-26 11:22
 */
@Controller
public class UserController {

    @Resource
    UserService userService;
    /**
     * 登录接口
     */
    @RequestMapping(value = "/subLogin")
    //    , method = RequestMethod.POST,produces = "application/json;charset=utf-8"
    public String login(String username, String password, Boolean isRememberMe, ModelMap model) {
        Subject subject = SecurityUtils.getSubject();
//        String username=String.valueOf(user.get("username"));
//        String password=String.valueOf(user.get("password"));
//        Boolean isRememberMe=Boolean.parseBoolean((String) user.get("isRememberMe"));
        if(isRememberMe==null){
            isRememberMe=false;
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username,password,isRememberMe);
        try {
            token.setRememberMe(isRememberMe);
            subject.login(token);
            User userTmp =userService.findByAccount(username);
//            Session session = subject.getSession();
//            session.setAttribute("user", userTmp);
//            session.setAttribute("userid", userTmp.getId());
            Subject currentUser = SecurityUtils.getSubject();
            Session session = currentUser.getSession();
            model.put("user",userTmp);
        } catch (AuthenticationException e) {
            return e.getMessage();
        }
        if (subject.hasRole("admin")) {
            return "index";
        }
        return "404";

    }

    @RequestMapping(value = "/login")
    public String login() {
        Subject subjec = SecurityUtils.getSubject();

        if (subjec != null && subjec.isAuthenticated()) {
            return "index";
        }
        return "login";
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    //    @ResponseBody
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/login";
    }

    @RequiresRoles("admin")
    @RequestMapping(value = "/testRole", method = RequestMethod.GET)
    @ResponseBody
    public String testRole() {
        return "testRole success";
    }

    /**
     * shiro通过注解实现角色授权
     */
    @RequiresPermissions("xxx")
    @RequiresRoles("admin1")
    @RequestMapping(value = "/testRole1", method = RequestMethod.GET)
    @ResponseBody
    public String testRole1() {
        return "testRole success";
    }

    /**
     * 通过在service.xml配置访问权限
     */
    @RequestMapping(value = "/testRoles", method = RequestMethod.GET)
    @ResponseBody
    public String testRoles() {
        return "test roles success";
    }

    @RequestMapping(value = "/testRoles1", method = RequestMethod.GET)
    @ResponseBody
    public String testRoles1() {
        return "test roles1 success";
    }

    @RequestMapping(value = "/testPerms", method = RequestMethod.GET)
    @ResponseBody
    public String testPerms() {
        return "test roles success";
    }

    @RequestMapping(value = "/testPerms1", method = RequestMethod.GET)
    @ResponseBody
    public String testPerms1() {
        return "test roles1 success";
    }


}
